var app = getApp()
const date = new Date();
const years = [];
const months = [];
const days = [];
const hours = [];
const minutes = [];
var app = getApp()
//获取年
for (let i = 1900; i <= date.getFullYear() + 5; i++) {
  years.push("" + i);
}
//获取月份
for (let i = 1; i <= 12; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  months.push("" + i);
}
//获取日期
for (let i = 1; i <= 31; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  days.push("" + i);
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serchList: [],
    title: '',
    mdid: "",
    sex: '男',
    items: [{
        name: '男',
        value: '男',
      },
      {
        name: '女',
        value: '女',
      }
    ],
    // 时间选择器
    time: '',
    multiArray: [years, months, days],
    multiIndex: [0, 9, 16, 10, 17],
    indexx: null,
    val: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var openid = options.openid
    var zid = options.zid
    that.setData({
      openid: openid,
      zid: zid
    })
    //查询所有处方
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.userchufanglog',
      data: {
        openid: openid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          list: res.data
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('11')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    console.log('22', that.data.title)

    if (that.data.title == "") {
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        memberval: ""
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  bindPickerChange2: function (e) {
    var that = this;
    var dex = e.detail.value;
    var kcid = that.data.arrkc[dex].id;
    that.setData({
      kcid: kcid,
      title: that.data.arrkc[dex].title,
    })

  },
  bindPickerChange3: function (e) {
    var that = this;
    var zid = e.currentTarget.dataset.zid
    var openid = that.data.openid
    var id = e.currentTarget.dataset.id
    var title = e.currentTarget.dataset.title
    var orderid =  e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/chufangxq/chufangxq?id=' + id + '&zid=' + zid + '&openid=' + openid + '&title=' + title + '&zid=' + zid+'&orderid='+orderid,
    })

  },
  radioChange: function (e) {
    console.log(e)
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var items = this.data.items;
    this.setData({
      sex: e.detail.value
    })
    var items = this.data.items;
    for (var i = 0; i < items.length; ++i) {
      items[i].checked = items[i].value == e.detail.value
    }
    console.log(items)
    this.setData({
      items: items
    });
  },
  radioChangeArr: function (e) {
    console.log(e)
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var list = this.data.list;
    this.setData({
      name: e.detail.value
    })
    var mdid = this.data.mdid
    for (var i = 0; i < list.length; ++i) {
      list[i].checked = list[i].name == e.detail.value
      if (list[i].name == e.detail.value) {
        mdid = mdid.concat(list[i].id);
      }
    }
    this.setData({
      list: list,
      mdid: mdid
    });
  },

  bindsubmit(e) {
    var that = this
    var val = e.detail.value
    val.mdid = that.data.mdid
    val.kcid = that.data.kcid
    val.name = that.data.name
    val.tid = that.data.tid
    val.yytime = that.data.yytime
    val.detailtime = that.data.detailtime
    val.year = that.data.year
    val.useropenid = that.data.useropenid
    var title = val.title
    var tel = val.tel
    var kcname = val.kcname
    var sex = that.data.sex
    var ddtime = val.ddtime
    var shijian = val.shijian
    var age = val.age

    if (title == '') {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none',
      })
      return false
    }
    if (age == '') {
      wx.showToast({
        title: '请填写年龄',
        icon: 'none',
      })
      return false
    }
    if (tel == '') {
      wx.showToast({
        title: '请填写联系方式',
        icon: 'none',
      })
      return false
    }
    app.util.request({
      'url': 'entry/wxapp/addtcyuyue',
      data: {
        val
      },
      success: function (res) {
        console.log(res)
        val.yyid = res.data
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        wx.setStorageSync('detail', val)
        wx.setStorageSync('yyid', res.data)
        prevPage.setData({
          memberval: val
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        })
      }
    })

    that.setData({
      title: title
    })
  },
  bindMultiPickerChange: function (e) {
    console.log(e)
    console.log('picker发送选择改变，携带下标为', e.detail.value)

    this.setData({
      multiIndex: e.detail.value
    })
    const index = this.data.multiIndex;
    const year = this.data.multiArray[0][index[0]];
    const month = this.data.multiArray[1][index[1]];
    const day = this.data.multiArray[2][index[2]];
    this.setData({
      time: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      shijian: e.detail.value
    })
  },
  serch: function (e) {
    var serch_text = e.detail.value
    var listarr = this.data.list
    console.log(listarr)
    var arr = []
    for (var i = 0; i < listarr.length; i++) {
      if (listarr[i].title.indexOf(serch_text) != -1) {
        arr.push(listarr[i])
      }
    }
    this.setData({
      serchList: arr
    })
    if (serch_text == '') {
      this.setData({
        serchList: []
      })
    }
  },
  close_btn: function (e) {
    this.setData({
      serch_text: ''
    })
  },
})